package leetcode.剑指offer.剑指II;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author ：zsy
 * @date ：Created 2022/10/13 8:19 PM
 * @description：
 */
public class 剑指_II_046_二叉树的右侧视图 {

    @Test
    public void test() {

    }

    class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> ans = new ArrayList<>();
            if (root == null) return ans;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while (!queue.isEmpty()) {
                int size = queue.size() - 1;
                for (int i = 0; i < size + 1; i++) {
                    TreeNode cur = queue.poll();
                    if (cur.left != null) {
                        queue.offer(cur.left);
                    }
                    if (cur.right != null) {
                        queue.offer(cur.right);
                    }
                    if (i == size) {
                        ans.add(cur.val);
                    }
                }
            }
            return ans;
        }
    }
}
